<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <style>
        #container {
            width: 520px;
            margin: 20px auto;
        }
        .input {
            margin: 20px 20px;
            width: 450px;
            height: 40px;
        }
        .input>label {
            display: inline-block;
            width: 150px;
            text-align: right;
        }
        a {
            text-decoration: none;
            color: darkcyan;
        }
        form+div {
            margin-top: 20px;
        }
        form+div>a {
            font-size: 1.2em;
        }
        .button {
            width: 520px;
            margin-top: 10px;
            text-align: center;
        }
        .button>div {
            text-align: left;
            margin: 10px 10px;
        }
        p.error {
            font-size: 14px;
            color: red;
            height: 16px;
        }
    </style>
</head>
<body>
    <div id="container">
        <h1>用户注册</h1>
        <hr>
        <p class="error">{{ hint }}</p>
        <form action="/register/" method="post">
            {% csrf_token %}
            <fieldset>
                <legend>用户信息</legend>
                <div class="input">
                    <label>用户名：</label>
                    <input type="text" name="username" value="{{ username }}" required minlength="6" maxlength="20">
                    <span></span>
                </div>
                <div class="input">
                    <label>密码：</label>
                    <input type="password" name="password">
                </div>
                <div class="input mobile">
                    <label>手机号：</label>
                    <input type="tel" name="tel" value="{{ tel }}">
                    <input type="button" id="sendBtn" value="发送验证码">
                </div>
                <div class="input">
                    <label>验证码：</label>
                    <input type="text" name="mobilecode">
                </div>
            </fieldset>
            <div class="button">
                <input type="submit" value="注册">
                <input type="reset" value="重置">
                <div>
                    <input type="checkbox" name="agreement">
                    我已经同意网站<a href="">用户协议及隐私政策</a>
                </div>
            </div>
        </form>
        <div>
            <a href="/">返回首页</a>
            <a href="/login/">返回登录</a>
        </div>
    </div>
    <script src="/static/js/jquery.min.js"></script>
    <script>
        $('#sendBtn').on('click', evt => {
            let tel = $(evt.target).prev().val().trim()
            if (/^1[3-9]\d{9}$/.test(tel)) {
                // 异步请求获取手机验证码
                // - 路径参数：---> /mobilecode/13100112233/ ---> ???
                // - 查询参数：---> /mobilecode/?tel=13100112233 ---> request.GET['tel']
                fetch('/mobilecode/' + tel + '/')
                    .then(resp => resp.json())
                    .then(json => {
                        alert(json.message)
                    })
            } else {
                alert('请输入有效的手机号')
            }
        })
        $('input[name=username]').on('blur', evt => {
            let $input = $(evt.target)
            let username = $input.val().trim()
            if (/^\w{6,20}$/.test(username)) {
                $.getJSON('/check/?username=' + username, json => {
                    if (json.is_valid) {
                        $input.next().text('✔︎').css('color', 'green')
                    } else {
                        $input.next().text('✘︎').css('color', 'red')
                    }
                })
            } else {
                $input.next().text('✘︎').css('color', 'red')
            }
        })
    </script>
</body>
</html>